<template>
    <div class="login-container">
        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form" hide-required-asterisk>
            <div class="login-title">
                <h3 class="title">后台管理系统</h3>
            </div>
            <el-form-item prop="username" class="login-item">
                <svg class="svg-container">
                    <use xlink:href="#icon-user"></use>
                </svg>
                <el-input class="login-input" v-model="loginForm.username" placeholder="用户名" />
            </el-form-item>
            <el-form-item prop="password" class="login-item">
                <svg class="svg-container">
                    <use xlink:href="#icon-pwd"></use>
                </svg>
                <el-input class="login-input" v-model="loginForm.password" show-password placeholder="密码" />
            </el-form-item>
            <el-button class="login-button" round type="primary" @click="loginClick">登录</el-button>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import type { UserLoginType } from "@/api/user/types";
import type { FormInstance, FormRules } from 'element-plus'
import { useUserStoreWithOut } from "@/store/modules/user";
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const { replace } = useRouter()

interface RuleForm {
    username: string
    password: string
}
const loginFormRef = ref<FormInstance>()
const userStore = useUserStoreWithOut()
const loginForm = ref({
    username: 'admin',
    password: '123456'
});
const rules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
});
const loginClick = () => {
    loginFormRef.value?.validate((valid, fields) => {
        if (valid) {
            const { username, password } = loginForm.value
            const params: UserLoginType = {
                username,
                password
            }
            userStore.login(params).then(res => {
                console.log(res)
                replace({
                    path: '/'
                })
            }).catch(err => {
                console.log(err)
                ElMessage.error(err)
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

</script>

<style lang="scss">
$bg: #2d3a4b;
$cursor: #fff;
$light_gray: #fff;

.login-container {
    width: 100%;
    height: 100%;
    background-color: $bg;
    display: flex;
    justify-content: center;

    .login-form {
        position: relative;
        width: 70%;
        max-width: 380px;
        padding: 160px 35px 0 35px;
        text-align: center;
        z-index: 10;

        .login-title {
            color: #fff;
            margin-bottom: 30px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            position: relative;
            text-align: center;

            .title {
                font-size: 26px;
                position: relative;
                display: inline-block;

                &::after {
                    content: '';
                    position: absolute;
                    bottom: -10px;
                    left: 50%;
                    transform: translateX(-50%);
                    width: 100%;
                    height: 3px;
                    background: linear-gradient(90deg, #4169E1, #4facfe);
                    border-radius: 3px;
                }
            }
        }

        .login-item {
            border: 1px solid rgba(255, 255, 255, 0.1);
            background: rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            color: #454545;

            .svg-container {
                width: 20px;
                height: 20px;
                margin: 0px 0px 0px 10px;
            }

            .login-input {
                width: 100%;
                height: 47px;
                flex: 1;

                .el-input__wrapper {
                    background: transparent;
                    border: 0px;
                    outline: none;
                    box-shadow: none;
                    padding: 1px 10px;

                    input {
                        color: #fff
                    }
                }
            }
        }

        .login-button {
            width: 100%;
            height: 40px;
            padding: 14px;
        }
    }
}
</style>